package com.hundsun.aitest.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hundsun.aitest.common.DirNameEnum;
import com.hundsun.aitest.service.BaoSongDBService;
import com.hundsun.aitest.util.oracle.OracleConnectUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.catalina.util.ParameterMap;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class BaoSongDBServiceImpl implements BaoSongDBService {


    public BaoSongDBServiceImpl() throws ClassNotFoundException {
    }

    @Override
    public String getCheckName(String checkCode) {

        String sql = String.format("SELECT CHECK_NAME FROM URP_DATACHECK_RULE WHERE CHECK_CODE='%s'", checkCode);
        JSONArray result = OracleConnectUtils.select(sql);

        return result.getJSONObject(0).getString("CHECK_NAME");
    }

    @Override
    public String getCheckRuleSql(String checkCode) {

        String sql = String.format("SELECT CHECK_SQL FROM URP_DATACHECK_RULE WHERE CHECK_CODE='%s'", checkCode);
        JSONArray result = OracleConnectUtils.select(sql);

        return result.getJSONObject(0).getString("CHECK_SQL");
    }

    @Override
    public JSONArray getReportList(String parentCode) {
        String sql = String.format("select distinct REPORT_CODE,PARENT_MODULE_CODE from  URP3.URP_DATACHECK_RULE where PARENT_MODULE_CODE like '%%%s%%'", parentCode);
        JSONArray result = OracleConnectUtils.select(sql);

        return result;
    }

    @Override
    public String getDirectory(String checkCode) {

        String sql = String.format("SELECT PARENT_MODULE_CODE FROM URP_DATACHECK_RULE WHERE CHECK_CODE='%s'", checkCode);
        JSONArray result = OracleConnectUtils.select(sql);

        String directory = result.getJSONObject(0).getString("PARENT_MODULE_CODE");
        String subNode = directory.split("\\.")[1];

       // DirNameMapper dirNameMapper = DirNameMapper.getInstance();
        directory = directory.replace(subNode, DirNameEnum.getDisplayNameByCode(subNode));

        directory = directory.replace(".", "/");

        return directory + "/";
    }

    @Override
    public String getTableMetaData(String checkCode) {

        String sql = String.format("SELECT REPORT_CODE FROM URP_DATACHECK_RULE WHERE CHECK_CODE='%s'", checkCode);
        JSONArray result = OracleConnectUtils.select(sql);

        String reportCode = result.getJSONObject(0).getString("REPORT_CODE");
        sql = String.format("SELECT DATA_TABLE_NAME from URP_REPORT_INFO WHERE REPORT_CODE='%s'", reportCode);
        result = OracleConnectUtils.select(sql);

        String tableName = result.getJSONObject(0).getString("DATA_TABLE_NAME");
        String ddl = OracleConnectUtils.metaData(tableName);

        return ddl;
    }

    @Override
    public JSONArray getColumnComments(String checkCode) {

        String sql = String.format("SELECT REPORT_CODE FROM URP_DATACHECK_RULE WHERE CHECK_CODE='%s'", checkCode);
        JSONArray result = OracleConnectUtils.select(sql);

        String reportCode = result.getJSONObject(0).getString("REPORT_CODE");
        sql = String.format("SELECT DATA_TABLE_NAME from URP_REPORT_INFO WHERE REPORT_CODE='%s'", reportCode);
        result = OracleConnectUtils.select(sql);

        String tableName = result.getJSONObject(0).getString("DATA_TABLE_NAME");
        JSONArray comments = OracleConnectUtils.columnComments(tableName);

        return comments;
    }

    @Override
    public JSONArray getCheckRules(String reportCode) {
        String sql = String.format("select CHECK_CODE from URP_DATACHECK_RULE where CHECK_TYPE=0 and REPORT_CODE='%s'", reportCode);
        JSONArray result = OracleConnectUtils.select(sql);

        JSONArray checkCodes = new JSONArray();
        for (Object obj : result) {
            if (obj instanceof JSONObject) {
                JSONObject jsonObject = (JSONObject) obj;
                String checkCode = jsonObject.getString("CHECK_CODE");
                if (checkCode != null && !checkCode.isEmpty()) {
                    checkCodes.add(checkCode);
                }
            }
        }

        return checkCodes;
    }

    @Override
    public Map<String, Object> getCheckRuleInterParams(String checkCode) {
        Map<String, Object> params = new HashMap<>();
        String reportCode = checkCode.split("\\.")[0];
        //--组织根获取：
        //-- 1：获取Business_report_code，001+二级模块id：URP_REPORT_INFO通过reportCode获取+reportcode+通过URP_MANAGER_INFO获得的管理人代码
        //-- 2：通过获取Business_report_code 查询urp_report_process拿到pro_ng_id
        //-- 3: 通过pro_ng_id ，查询urp_process_node_config 拿到data_belong和拿到data_belong_en
        String businessReportCode = "001";
        //二级模块id获取：URP_REPORT_INFO通过reportCode获取
        String moduleSql = String.format("select PARENT_MODULE_CODE from URP_REPORT_INFO where REPORT_CODE='%s'", reportCode);
        String parentModuleCode = OracleConnectUtils.select(moduleSql).getJSONObject(0).getString("PARENT_MODULE_CODE");
        //获得管理人代码，通过URP_MANAGER_INFO获得的管理人代码，这里可以写实，用招商基金：cisp_org_code:12334567,fin_ins_code:11002255114401
        String managerCode = parentModuleCode.contains("CISP") ? "12334567" : "11002255114401";
        businessReportCode = businessReportCode + "." + parentModuleCode + "." + reportCode + "." + managerCode;
        String proNgIdSql = String.format("select DATA_BELONG,DATA_BELONG_EN from URP_REPORT_PROCESS rp INNER JOIN urp_process_node_config upnc on rp.PRO_NG_ID=upnc.PRO_NG_ID and rp.BUSINESS_REPORT_CODE='%s'", businessReportCode);
        String dataBelong = OracleConnectUtils.select(proNgIdSql).getJSONObject(0).getString("DATA_BELONG");
        String dataBelongEn = OracleConnectUtils.select(proNgIdSql).getJSONObject(0).getString("DATA_BELONG_EN");
        params.put("期望","error_info=操作成功！");
        params.put("env_id", "001");
        params.put("user_no", "admin");
        params.put("data_belong", dataBelong);
        params.put("data_belong_en", dataBelongEn);
        params.put("report_code", reportCode);
        //后续从前一部传入
        params.put("report_flow_id","varc_data_0_id");
        params.put("parent_module_code", parentModuleCode);
        params.put("rootModuleCode", parentModuleCode.split("\\.")[0]);


        return params;
    }

    @Override
    public JSONArray getReportInfo(String reportCode) {
        String sql = String.format("SELECT * FROM URP_REPORT_INFO WHERE REPORT_CODE='%s'", reportCode);
        JSONArray result = OracleConnectUtils.select(sql);
        return result;
    }


    @Override
    public JSONArray getReportProcess(String reportCode) {
        String moduleSql = String.format("select PARENT_MODULE_CODE from URP_REPORT_INFO where REPORT_CODE='%s'", reportCode);
        String parentModuleCode = OracleConnectUtils.select(moduleSql).getJSONObject(0).getString("PARENT_MODULE_CODE");
        //获得管理人代码，通过URP_MANAGER_INFO获得的管理人代码，这里可以写实，用招商基金：cisp_org_code:12334567,fin_ins_code:11002255114401
        String managerCode = parentModuleCode.contains("CISP") ? "12334567" : "11002255114401";
        String sql = String.format("select * from URP_REPORT_PROCESS where REPORT_CODE='%s' and BUSINESS_REPORT_CODE like '%%%s%%'", reportCode,managerCode);
        JSONArray result = OracleConnectUtils.select(sql);
        return result;
    }



}
